<?php
/**
 * 币种管理
 */
namespace app\api\server;
use app\api\model\Currency as CurrencyModel;

class Currency extends CurrencyModel
{
    //获取动态价格
    public static function dynamic_price(){
        //查询所有在使用且非自定义价格
        $all = self::where('status',1)->whereIn('type','1,2')->select();
        if(empty($all)) return false;
        $all = $all->toArray();

        //获取binance代币价格
        $url = 'http://trc20dapp.thedaoio.com/api/task/price';
        $client = new \GuzzleHttp\Client();
        //币种价格
        $response = $client->request('GET', $url, [
            'headers' => [
                'accept' => 'application/json',
            ],
        ]);
        //json 订单 转数组
        $price_all = json_decode($response->getBody(),true);
        $price_arr = [];
        foreach ($price_all as $v){ //循环价格
            $price_arr[$v['symbol']] = $v['price'];
        }

        //循环所有币种
        foreach ($all as $v){
            if(!empty($price_arr[$v['currency_name'] . $v['price_currency']])) {    //币安更新价格
                $update['current_price'] = $price_arr[$v['currency_name'] . $v['price_currency']];
                if (date('d', time()) != $v['day']) {
                    $update['today_price'] = $price_arr[$v['currency_name'] . $v['price_currency']];  //更新今日价格
                    $update['day'] = date('d', time());  //当天日期
                }
                //判断现在释放是次日
                self::where('id', $v['id'])->update($update);

            }
        }

    }


}